/*
 * Copyright 2007. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the GNU Lesser General Public License, Version 2. You
 * can find a copy of the license at:
 * 
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */
package com.sinai.mshab.client.user;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sinai.mshab.client.pages.Login;
import com.sinai.mshab.client.ui.aggregate.FormData;
import com.sinai.mshab.client.ui.aggregate.PageContainer;
import com.sinai.mshab.client.ui.basic.Warning;

/**
 * This is a validator given to the Login page during creation
 * this will authenticate the users informtion entered at the
 * login screen though LDAP.
 * @author serge
 *
 */
public class LDAPValidator implements UserValidator {

	String the_username;

	/**
	 * Will attempt to authenticate username and password with ldap.
	 */
	public void authenticate(String username, String password) {
		the_username = username;
		FormData.getInstance().getService().authenticate(username, password,
				new UserExistsCallback());
	}

	/**
	 * Catches true or false returned from the server telling us if the user
	 * exists or not.
	 * 
	 * @author serge
	 */
	public class UserExistsCallback implements AsyncCallback {

		public void onFailure(Throwable caught) {
			// do nothing
		}

		// if the user exists, get their permissions.
		public void onSuccess(Object result) {
			boolean user_exists = ((Boolean) result).booleanValue();
			if (user_exists) {
				System.out.println("User exists, fetching permissions");
				FormData.getInstance().getService().getUserPermission(
						the_username, new PermissionCallback());
			} else {
				new Warning(
						" The provided username and password are not valid",
						100, 200);
			}
		}

	}

	/**
	 * Retrieves the user permissions from the server and makes a cookie.
	 * 
	 * @author serge
	 */
	public class PermissionCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {
			GWT.log("Error ", caught);
			caught.printStackTrace();
			new Warning(caught.getMessage() + "  " + caught.toString(), 300,
					100);
		}

		// take action based on success of login attempt
		public void onSuccess(Object result) {

			String permission = (String) result;

			System.out.println("Got permission it's: " + result);

			// login successful
			Cookies.removeCookie("updb cookie");
			Date today = new Date();
			Date expire_date = new Date(today.getTime() + 1000 * 60 * 60 * 10);
			Cookies.setCookie(FormData.getServletUrl() + " cookie", permission,
					expire_date);

			// if pending.exists load pending, else
			if (PageContainer.hasPendingPage()) {
				String pending_page = PageContainer.getPending_page();
				History.newItem(pending_page);
			} else {
				Login.userIsValid();
			}
		}
	}
}
